package cw.spring.security.acl.poc.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Pattern;

@Entity
@Table(name = "NorthGroup", uniqueConstraints = { @UniqueConstraint(columnNames = { "id" }) })
public class NorthGroupModel {

  private String name; // name of the group
  private List<NorthUserModel> users;


  private long id;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }
  
  @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
  @JoinTable(name = "GroupUser", joinColumns = @JoinColumn(name = "group_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
  public List<NorthUserModel> getUsers() {
    return users;
  }
  
  public void setUsers(List<NorthUserModel> users) {
    this.users = users;
  }

  @NotNull
  @Length(min = 1, max = 256, message = "The group name must have a length superior or equals to 1")
  @Pattern(regex = "[a-zA-Z0-9_]*", message = "The group name must be composed of characters, numbers or underscores (upper or lower case)")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}
